package com.vhall.component.entity.data.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;

/**
 * @author shimingming
 * @since 2023/6/6 17:29
 */
@Getter
@AllArgsConstructor
public enum DurationDistributionEnum {


    /**
     * 左闭右开
     */

    TIME1(0, 0, 5, "0-5分钟"),
    TIME2(1, 5, 10, "5-10分钟"),
    TIME3(2, 10, 15, "10-15分钟"),
    TIME4(3, 15, 20, "15-20分钟"),
    TIME5(4, 20, 25, "20-25分钟"),
    TIME6(5, 25, 30, "25-30分钟"),
    TIME7(6, 30, 35, "30-35分钟"),
    TIME8(7, 35, 40, "35-40分钟"),
    TIME9(8, 40, 45, "40-45分钟"),
    TIME10(9, 45, Integer.MAX_VALUE, "45分钟以上"),


    ;
    private final Integer key;
    private final Integer start;
    private final Integer end;

    private final String description;

    public static Integer getKey(Integer value) {
        DurationDistributionEnum parse = parse(value);
        if (parse != null) {
            return parse.getKey();
        }
        return null;
    }

    public static String getDescriptionByKey(Integer value) {
        for (DurationDistributionEnum anEnum : DurationDistributionEnum.values()) {
            if (anEnum.getKey().equals(value)) {
                return anEnum.getDescription();
            }
        }
        return "";
    }

    public static DurationDistributionEnum parse(Integer value) {
        for (DurationDistributionEnum anEnum : DurationDistributionEnum.values()) {

            int start = new BigDecimal(anEnum.getStart()).multiply(new BigDecimal("60")).intValue();
            int end = anEnum.getEnd() == Integer.MAX_VALUE ? Integer.MAX_VALUE : new BigDecimal(anEnum.getEnd()).multiply(new BigDecimal("60")).intValue();


            if (value >= start && value < end) {
                return anEnum;
            }

        }
        return null;
    }
}
